<?php



class Webmix_Plugin_Core

{

    public function setCompanyAddCount($event){

        $payload = $event->getPayload();

        

        $table = Engine_Api::_()->getItemTable('webmix_company');

        

        if(isset($payload['company']) && isset($payload['company_new'])){

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $company = Engine_Api::_()->getItem('webmix_company',$payload['company']);

                $company->add_count = $company->add_count - 1;

                $company->save();

                $company_new = Engine_Api::_()->getItem('webmix_company',$payload['company_new']);

                $company_new->add_count = $company_new->add_count + 1;

                $company_new->save();

                                             

                $db->commit();

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            }  

        }

        elseif(isset($payload['company_create'])){

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $company = Engine_Api::_()->getItem('webmix_company',$payload['company_create']);

                $company->add_count = $company->add_count + 1;

                $company->save();

                

                $db->commit();                

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            }  

        }

        elseif(isset($payload['company_delete'])){

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $company = Engine_Api::_()->getItem('webmix_company',$payload['company_delete']);

                $company->add_count = $company->add_count - 1;

                $company->save();

                

                $db->commit();                

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            }  

        }

        

    }

    public function setCountryCompanyCount($event){

    

        $payload = $event->getPayload();

        

        $table = Engine_Api::_()->getItemTable('webmix_country');

        

        if(isset($payload['country']) && isset($payload['country_new'])){

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $country = Engine_Api::_()->getItem('webmix_country',$payload['country']);

                $country->company_count = $country->company_count - 1;

                $country->save();

                $country_new = Engine_Api::_()->getItem('webmix_country',$payload['country_new']);
				if($country_new){

                $country_new->company_count = $country_new->company_count + 1;

                $country_new->save();
}
                                             

                $db->commit();

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            }  

        }        

        elseif(isset($payload['country_create'])){

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $country = Engine_Api::_()->getItem('webmix_country',$payload['country_create']);

                $country->company_count = $country->company_count + 1;

                $country->save();

                

                $db->commit();                

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            }  

        }

        elseif(isset($payload['country_delete'])){

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $country = Engine_Api::_()->getItem('webmix_country',$payload['country_delete']);

                $country->company_count = $country->company_count - 1;

                $country->save();

                

                $db->commit();                

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            }  

        }        

    }

    public function deleteBlockofCompany($event){

        $payload = $event->getPayload();

        $company = $payload['company'];

        $table = Engine_Api::_()->getItemTable('webmix_block');

        if(isset($company)){

            

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $table->delete(array('company_id = ?' => $company));

                $db->commit();

             

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            } 

        }         

                

    }   

    

    //delete block and update company added count

    public function deleteBlockofWebmix($event){

        $payload = $event->getPayload();

        $webmix = $payload['webmix'];

        $table = Engine_Api::_()->getItemTable('webmix_block');

        if(isset($webmix)){

            

            //update company added count

            $companyTable = Engine_Api::_()->getItemTable('webmix_company');

            $companies = $companyTable->getCompanybyWebmix($webmix);

            foreach($companies as $company){

                $db_company = $companyTable->getAdapter();

                $db_company->beginTransaction();

                try{
					if($company){

	                    $company->add_count = $company->add_count - 1;
	
	                    $company->save();
}

                    $db_company->commit();

                }

                catch(Exception $e)

                {

                    $db_company->rollback();

                    throw $e;

                } 

            }    

            

            //delete block           

            $db = $table->getAdapter();

            $db->beginTransaction() ;

            try{

                $table->delete(array('webmix_id = ?' => $webmix));

                $db->commit();

             

            }   

            catch(Exception $e){

                $db->rollBack();

                throw $e;

            } 

        }         

                

    } 

    

}